# -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

class Star_3():

  def __init__(self, H, D1, D2, X_N, Y_N):
    # 几何形状参数

    self.H = H           # 胞元间连杆长度

    # 星形外接圆参数
    self.D1 = D1         # 内圆直径
    self.D2 = D2         # 外圆直径
    self.R1 = 0.5 * D1   # 内圆半径
    self.R2 = 0.5 * D2   # 外圆半径

    # 大小规格（阵列数目）
    self.X_N = X_N      
    self.Y_N = Y_N

    # 阵列间距
    self.X_SPACE = 3 ** 0.5 * H
    self.Y_SPACE = 3 * H
  
  # 生成三角星
  def createStar(self):

    def sin(theta):
      return math.sin(theta / 180.0 * math.pi)

    def cos(theta):
      return math.cos(theta / 180.0 * math.pi)

    H = self.H
    R1 = self.R1
    R2 = self.R2

    # 三角星的六个顶点坐标（从正上方开始编号为1，逆时针编号）
    point_1 = (R2 * cos(90), R2 * sin(90), 0)
    point_2 = (R1 * cos(150), R1 * sin(150), 0)
    point_3 = (R2 * cos(210), R2 * sin(210), 0)
    point_4 = (R1 * cos(270), R1 * sin(270), 0)
    point_5 = (R2 * cos(330), R2 * sin(330), 0)
    point_6 = (R1 * cos(390), R1 * sin(390), 0)

    line_1 = (point_1, point_2)
    line_2 = (point_2, point_3)
    line_3 = (point_3, point_4)
    line_4 = (point_4, point_5)
    line_5 = (point_5, point_6)
    line_6 = (point_6, point_1)

    # 三角星外围的三根连杆
    link_point_1 = (0.5 * H * cos(150), 0.5 * H * sin(150), 0)
    link_point_2 = (0.5 * H * cos(270), 0.5 * H * sin(270), 0)
    link_point_3 = (0.5 * H * cos(390), 0.5 * H * sin(390), 0)

    link_line_1 = (link_point_1, point_2)
    link_line_2 = (link_point_2, point_4)
    link_line_3 = (link_point_3, point_6)

    lines = [line_1, line_2, line_3,
            line_4, line_5, line_6,
            link_line_1, link_line_2, link_line_3]

    return lines

  # x轴镜像
  def mirrorX(self, lines):
    mirrorLines = []
    for line in lines:
      point_1 = (line[0][0], -line[0][1], line[0][2])
      point_2 = (line[1][0], -line[1][1], line[1][2])

      mirrorLines.append((point_1, point_2))
    return mirrorLines

  # 生成倒三角星
  def createDownwardStar(self):

    starLines = self.createStar()
    downwardStarLines = self.mirrorX(starLines)

    return downwardStarLines

  # 沿x轴、y轴阵列线段
  def arrayLines(self, x_n, y_n, lines):

    X_SPACE = self.X_SPACE
    Y_SPACE = self.Y_SPACE

    X_arrayLines = []
    Y_arrayLines = []

    # 胞元沿x轴进行阵列
    for i in range(x_n):

      for item in lines:

        point_x_1 = (item[0][0] + X_SPACE * i, item[0][1], item[0][2])
        point_x_2 = (item[1][0] + X_SPACE * i, item[1][1], item[1][2])
        
        X_arrayLines.append((point_x_1, point_x_2))
    
    # 胞元沿y轴进行阵列
    for j in range(y_n):

      for each in X_arrayLines:

        point_y_1 = (each[0][0], each[0][1] + Y_SPACE * j, each[0][2])
        point_y_2 = (each[1][0], each[1][1] + Y_SPACE * j, each[1][2])

        Y_arrayLines.append((point_y_1, point_y_2))

    return Y_arrayLines

  # 移动线段位置
  def move(self, offset_x, offset_y, lines):

    res = []

    for line in lines:
      point_1 = (line[0][0] + offset_x, line[0][1] + offset_y, 0)
      point_2 = (line[1][0] + offset_x, line[1][1] + offset_y, 0)
      res.append((point_1, point_2))

    return res

  def printParameters(self):

    print(" ")

    # 打印参数
    print("H: %s " % self.H)
    print("D1: %s " % self.D1)
    print("D2: %s " % self.D2)
    print("X_N: %d " % self.X_N)
    print("Y_N: %d " % self.Y_N)

  # 创建Part
  def part(self):

    H = self.H
    X_N = self.X_N
    Y_N = self.Y_N

    starLines = self.createStar()
    downwardStarLines = self.createDownwardStar()
    
    # 将三角星和倒三角星分四次阵列
    starLines_1 = starLines
    array_1 = self.arrayLines(X_N, Y_N, starLines_1)

    starLines_2 = self.move(-0.5 * 3 ** 0.5 * H, 0.5 * H, downwardStarLines)
    array_2 = self.arrayLines(X_N + 1, Y_N, starLines_2)

    starLines_3 = self.move(-0.5 * 3 ** 0.5 * H, 1.5 * H, starLines)
    array_3 = self.arrayLines(X_N + 1, Y_N, starLines_3)

    starLines_4 = self.move(0, 2 * H, downwardStarLines)
    array_4 = self.arrayLines(X_N, Y_N, starLines_4)

    lines = array_1 + array_2 + array_3 + array_4

    lines_tuple = tuple(lines)

    mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=DEFORMABLE_BODY)

    mdb.models['Model-1'].parts['Part-1'].WirePolyLine(mergeType=IMPRINT, meshable=ON, 
        points=lines_tuple)

    mdb.models['Model-1'].parts['Part-1'].Set(edges=
        mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#3 ]', 
        ), ), name='Wire-2-Set-1')

    self.printParameters()

def main(H, D1, D2, X_N, Y_N):
  star_1 = Star_3(H, D1, D2, X_N, Y_N)
  star_1.part()

if __name__ == '__main__':

  main(H=10.0, D1=2.0, D2=6.0, X_N=5, Y_N=3)

# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153
